﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace LCL.CodeHelper
{
    public class VSProject
    {
        public void AddMethodToClass(string ClassFile, string strContent)
        {
            if (File.Exists(ClassFile))
            {
                string text = File.ReadAllText(ClassFile, Encoding.Default);
                if (text.IndexOf(" class ") > 0)
                {
                    int num = text.LastIndexOf("}");
                    string text2 = text.Substring(0, num - 1);
                    int num2 = text2.LastIndexOf("}");
                    string str = text.Substring(0, num2 - 1);
                    string value = str + "\r\n" + strContent + "\r\n}\r\n}";
                    StreamWriter streamWriter = new StreamWriter(ClassFile, false, Encoding.Default);
                    streamWriter.Write(value);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }
        }
        //添加类到项目文件
        public static void AddClass2012(string filename, string classname)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filename);
            foreach (XmlElement xmlElement in xmlDocument.DocumentElement.ChildNodes)
            {
                if (xmlElement.Name == "ItemGroup")
                {
                    string arg_51_0 = xmlElement.ChildNodes[0].InnerText;
                    string name = xmlElement.ChildNodes[0].Name;
                    if (name == "Compile")
                    {
                        XmlElement xmlElement2 = xmlDocument.CreateElement("Compile", xmlDocument.DocumentElement.NamespaceURI);
                        xmlElement2.SetAttribute("Include", classname);
                        xmlElement.AppendChild(xmlElement2);
                        break;
                    }
                }
            }
            xmlDocument.Save(filename);
        }
        //添加DLL引用到项目文件
        public static void AddReference2012(string filename, string dllName, string dllPath, string SpecificVersion="")
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filename);
            foreach (XmlElement xmlElement in xmlDocument.DocumentElement.ChildNodes)
            {
                if (xmlElement.Name == "ItemGroup")
                {
                    string arg_51_0 = xmlElement.ChildNodes[0].InnerText;
                    string name = xmlElement.ChildNodes[0].Name;
                    if (name == "Reference")
                    {
                        XmlElement xmlElement2 = xmlDocument.CreateElement("Reference", xmlDocument.DocumentElement.NamespaceURI);
                        xmlElement2.SetAttribute("Include", dllName);

                        XmlElement xmlElement3 = xmlDocument.CreateElement("HintPath", xmlDocument.DocumentElement.NamespaceURI);
                        xmlElement3.InnerText = dllPath;
                        if (!string.IsNullOrWhiteSpace(SpecificVersion)) 
                        {
                            XmlElement xmlElement4 = xmlDocument.CreateElement("SpecificVersion", xmlDocument.DocumentElement.NamespaceURI);
                            xmlElement3.InnerText = SpecificVersion;
                            xmlElement2.AppendChild(xmlElement4);
                        }
                        xmlElement2.AppendChild(xmlElement3);
                        xmlElement.AppendChild(xmlElement2);
                        break;
                    }
                }
            }
            xmlDocument.Save(filename);
        }
        //添加项目引用到项目文件
        public static void AddProjectReference2012(string filename, string projectName, string projectGuid, string projectPath)
        {
            int project = 1;
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filename);
            foreach (XmlNode xmlElement in xmlDocument.DocumentElement.ChildNodes)
            {
                if (xmlElement.Name == "ItemGroup")
                { 
                   string name = xmlElement.ChildNodes[0].Name;
                   if (name == "ProjectReference")
                   {
                       project = 2;
                       XmlElement xmlElement2 = xmlDocument.CreateElement("ProjectReference", xmlDocument.DocumentElement.NamespaceURI);
                       xmlElement2.SetAttribute("Include", projectPath);

                       XmlElement xmlElement3 = xmlDocument.CreateElement("Name", xmlDocument.DocumentElement.NamespaceURI);
                       xmlElement3.InnerText = projectName;

                       XmlElement xmlElement4 = xmlDocument.CreateElement("Project", xmlDocument.DocumentElement.NamespaceURI);
                       xmlElement4.InnerText = projectGuid;

                       xmlElement2.AppendChild(xmlElement3);
                       xmlElement2.AppendChild(xmlElement4);

                       xmlElement.AppendChild(xmlElement2);
                   }
                }
            }
            if (project == 1)
            {
                XmlElement xmlElement1 = xmlDocument.CreateElement("ItemGroup", xmlDocument.DocumentElement.NamespaceURI);
                XmlElement xmlElement2 = xmlDocument.CreateElement("ProjectReference", xmlDocument.DocumentElement.NamespaceURI);
                xmlElement2.SetAttribute("Include", projectPath);
                XmlElement xmlElement3 = xmlDocument.CreateElement("Name", xmlDocument.DocumentElement.NamespaceURI);
                xmlElement3.InnerText = projectName;
                XmlElement xmlElement4 = xmlDocument.CreateElement("Project", xmlDocument.DocumentElement.NamespaceURI);
                xmlElement4.InnerText = projectGuid;
                xmlElement1.AppendChild(xmlElement2);
                xmlElement2.AppendChild(xmlElement3);
                xmlElement2.AppendChild(xmlElement4);

                xmlDocument.DocumentElement.AppendChild(xmlElement1);
            }
            xmlDocument.Save(filename);
        }
    }
}
